<template>
  <el-main>
    <!-- 搜索栏 -->
    <el-form :model="lookParm" label-width="80px" :inline="true" size="mini">
      <el-form-item label="读者证号">
        <el-input v-model="lookParm.username"></el-input>
      </el-form-item>
      <el-form-item label="读者姓名">
        <el-input v-model="lookParm.learnNum"></el-input>
      </el-form-item>
      <el-form-item label="图书名称">
        <el-input v-model="lookParm.bookName"></el-input>
      </el-form-item>
      <el-form-item label="借书状态">
        <el-select v-model="lookParm.borrowStatus" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="异常状态">
        <el-select v-model="lookParm.returnStatus" placeholder="请选择">
          <el-option
            v-for="item in exceoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="到期状态">
        <el-select v-model="lookParm.timeStatus" placeholder="请选择">
          <el-option
            v-for="item in timetions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="lookParm.applyStatus" placeholder="请选择">
          <el-option
            v-for="item in applytions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" round icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button size="small" type="danger" round icon="el-icon-close" @click="resetBtn">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      size="small"
      ref="tables"
      :height="tableHeight"
      :data="tableData"
      border
      stripe
    >
      <!--      <el-table-column fixed type="selection" width="55"></el-table-column>-->
      <el-table-column fixed prop="bookName" show-overflow-tooltip label="图书名称"></el-table-column>
      <el-table-column prop="bookPlaceNum" label="书架号"></el-table-column>
      <el-table-column prop="username" label="读者证号"></el-table-column>
      <el-table-column prop="learnNum" label="读者"></el-table-column>
      <el-table-column prop="className" show-overflow-tooltip label="班级"></el-table-column>
      <el-table-column prop="phone" show-overflow-tooltip label="电话"></el-table-column>
      <el-table-column prop="borrowTime" show-overflow-tooltip label="借书时间"></el-table-column>
      <el-table-column prop="returnTime" show-overflow-tooltip label="还书时间"></el-table-column>
      <el-table-column prop="applyText" label="审核备注"></el-table-column>
      <el-table-column prop="excepionText" label="还书备注"></el-table-column>
      <el-table-column prop="refuseText" show-overflow-tooltip label="拒绝备注"></el-table-column>
      <el-table-column
        prop="borrowStatus"
        label="借书状态"
        align="center"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.borrowStatus == '0'">审核中</el-tag>
          <el-tag type="primary" v-if="scope.row.borrowStatus == '1'">在借中</el-tag>
          <el-tag type="success" v-if="scope.row.borrowStatus == '2' && scope.row.returnStatus=='1'">已还</el-tag>
          <el-tag type="danger" v-if="scope.row.borrowStatus == '2' && scope.row.returnStatus=='2'">异常(已还)</el-tag>
          <el-tag type="danger" v-if="scope.row.borrowStatus == '2' && scope.row.returnStatus=='3'">丢失(已还)</el-tag>
          <el-tag type="danger" v-if="scope.row.borrowStatus == '3'">拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="timeStatus"
        label="到期状态"
        align="center"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.timeStatus == '0'">未到期</el-tag>
          <el-tag type="danger" v-if="scope.row.timeStatus == '1'">到期</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="applyStatus"
        label="审核状态"
        align="center"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.applyStatus == '0'">待审核</el-tag>
          <el-tag type="success" v-if="scope.row.applyStatus == '1'">已审核</el-tag>
          <el-tag type="danger" v-if="scope.row.applyStatus == '2'">拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" v-if="this.userType=='1'" align="center" width="230px" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.applyStatus=='0'" v-permission="['sys:borrow:examine']" type="primary"
                     icon="el-icon-edit" size="small"
                     @click="examinBtn(scope.row)"
          >同意借阅
          </el-button>
          <el-button v-permission="['sys:borrow:addTime']" v-if="scope.row.borrowStatus=='1'" icon="el-icon-plus"
                     type="success" size="small" @click="addTimeBtn(scope.row)"
          >续期
          </el-button>
          <el-button v-permission="['sys:borrow:refuse']"
                     v-if="scope.row.applyStatus=='0'" icon="el-icon-close" type="danger"
                     size="small" @click="refuseBtn(scope.row)"
          >拒绝借阅
          </el-button>
        </template>
      </el-table-column>
      <el-table-column v-if="this.userType=='0'" align="center" width="110px" label="操作">
        <template slot-scope="scope">
          <el-button v-permission="['sys:borrow:addTime']" v-if="scope.row.borrowStatus=='1'" icon="el-icon-plus"
                     type="success" size="small" @click="addTimeBtn(scope.row)"
          >续期
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page.sync="lookParm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="lookParm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="lookParm.total"
      background
    >
    </el-pagination>
    <!--续期弹框-->
    <sys-dialog
      :title="dialog.title"
      :visible="dialog.visible"
      :width="dialog.width"
      :height="dialog.height"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <el-form
          :model="addTime"
          ref="addTime"
          :rules="rules"
          label-width="80px"
          :inline="false"
          size="small"
        >
          <el-form-item prop="returnTime" label="续期时间">
            <el-date-picker v-model="addTime.returnTime" type="datetime" placeholder="选择续期日期"></el-date-picker>
          </el-form-item>
        </el-form>
      </div>
    </sys-dialog>
    <!--审核拒绝弹框-->
    <sys-dialog
      :title="refuseDialog.title"
      :width="refuseDialog.width"
      :height="refuseDialog.height"
      :visible="refuseDialog.visible"
      @onClose="onRefuseClose"
      @onConfirm="onRefuseConfirm"
    >
      <div slot="content">
        <el-form
          :model="refuseExamine"
          ref="refuseExamineRef"
          :rules="rules"
          label-width="80px"
          size="small"
        >
          <el-form-item prop="refuseText" label="拒绝备注">
            <el-input
              type="textarea"
              v-model="refuseExamine.refuseText"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </sys-dialog>
  </el-main>
</template>
<script>
import { getLookBorrowListApi, examinBookApi, addBookTimeApi, refuseExamineBooksAPi } from '@/api/borrow'
import { getUserId, getUserType } from '@/utils/auth'
import sysDialog from '@/components/dialog/SysDialog'

export default {
  components: {
    sysDialog
  },
  data () {
    return {
      userType: getUserType(),
      addTime: {
        borrowId: '',
        returnTime: ''
      },
      dialog: {
        title: '借书续期',
        visible: false,
        width: 330,
        height: 66
      },
      refuseDialog: {
        title: '拒绝审核',
        visible: false,
        width: 500,
        height: 88
      },
      refuseExamine: {
        borrowId: '',
        bookId: '',
        refuseText: ''
      },
      rules: {
        returnTime: [{
          trigger: 'change',
          required: true,
          message: '请选择续期时间'
        }],
        refuseText: [{
          trigger: 'blur',
          required: true,
          message: '请填写拒绝审核备注!'
        }]
      },
      tableData: [],
      applytions: [
        {
          value: '0',
          label: '待审核',
        },
        {
          value: '1',
          label: '已审核',
        },
        {
          value: '2',
          label: '拒绝',
        },
      ],
      timetions: [
        {
          value: '1',
          label: '到期',
        },
        {
          value: '0',
          label: '未到期',
        },
      ],
      exceoptions: [
        {
          value: '1',
          label: '正常还书',
        },
        {
          value: '2',
          label: '异常还书',
        },
        {
          value: '3',
          label: '丢失',
        }
      ],
      options: [
        {
          value: '0',
          label: '审核中',
        },
        {
          value: '1',
          label: '在借中',
        },
        {
          value: '2',
          label: '已还',
        },
        {
          value: '3',
          label: '拒绝',
        },
      ],
      lookParm: {
        userId: getUserId(),
        total: 0,
        currentPage: 1,
        pageSize: 10,
        username: '',
        learnNum: '',
        bookName: '',
        applyStatus: '',
        borrowStatus: '',
        returnStatus: '',
        timeStatus: '',
        refuseText:''
      },
      tableHeight: 0
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 250
    })
    this.getLookBorrowList()
  },
  methods: {
    onRefuseClose () {
      this.refuseDialog.visible = false
    },
    onRefuseConfirm () {
      this.$refs.refuseExamineRef.validate(async valid => {
        if (valid) {
          let res = await refuseExamineBooksAPi(this.refuseExamine)
          if (res && res.code == 200) {
            this.$message({ type: 'success', message: res.msg })
            await this.getLookBorrowList()
            this.refuseDialog.visible = false
            window.location.reload()
          }
        }
      })
    },
    //拒绝借阅
    refuseBtn (row) {
      this.refuseExamine.borrowId = row.borrowId
      this.refuseExamine.bookId = row.bookId
      this.refuseDialog.visible = true
    },
    onClose () {
      this.dialog.visible = false
    },
    onConfirm () {
      this.$refs.addTime.validate(async valid => {
        if (valid) {
          let res = await addBookTimeApi(this.addTime)
          if (res && res.code == 200) {
            this.$message({ type: 'success', message: res.msg })
            await this.getLookBorrowList()
            this.dialog.visible = false
            window.location.reload()
          }
        }
      })

    },
    //借书续期按钮
    addTimeBtn (row) {
      this.addTime.borrowId = row.borrowId
      this.dialog.visible = true
    },
    //审核按钮
    async examinBtn (row) {
      let confirm = await this.$myconfirm('确定审核吗?')
      {
        if (confirm) {
          let res = await examinBookApi({ borrowId: row.borrowId })
          if (res && res.code == 200) {
            this.getLookBorrowList()
            window.location.reload()
            this.$message({ type: 'success', message: res.msg })
          }
        }
      }
    },
    searchBtn () {
      this.getLookBorrowList()
    },
    resetBtn () {
      this.lookParm.username = ''
      this.lookParm.learnNum = ''
      this.lookParm.bookName = ''
      this.lookParm.applyStatus = ''
      this.lookParm.borrowStatus = ''
      this.lookParm.returnStatus = ''
      this.lookParm.timeStatus = ''
      this.getLookBorrowList()
    },
    currentChange (val) {
      this.lookParm.currentPage = val
      this.getLookBorrowList()
    },
    sizeChange (val) {
      this.lookParm.pageSize = val
      this.getLookBorrowList()
    },
    async getLookBorrowList () {
      let res = await getLookBorrowListApi(this.lookParm)
      if (res && res.code == 200) {
        this.tableData = res.data.records
        this.lookParm.total = res.data.total
      }
    },
  },
}
</script>
<style lang="scss" scoped>
</style>
